<?php
/**
 * Created by Jasmine.
 * Date: 2016/11/10
 * description:
 * Address: 浙江宣逸网络科技有限公司
 */

namespace Admin\Controller;


use Admin\BaseController;
use Think\Page;

class AllocationController extends BaseController
{


    /**
     * 社群积分配置
     */
    public function socialPoint(){
        $optType = C('OPT_TYPE');

        $count = M('cfg_value_get',null,DB_MAIN_CFG)->where("point >= 0")->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        $data = M('cfg_value_get',null,DB_MAIN_CFG)->where("point >= 0")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($data as $key => $val){
            $data[$key]['itemName'] = $optType[$val['action']];
        }
        $this->smarty->assign("list",$data);
        $this->smarty->assign('limit',$limit);
        $this->smarty->display("Allocation/socialPoint.html");
    }

    /**
     * 配置社区积分
     */
    public function configurePoint(){
        $dataFailure['status']=1;
        $dataSuccess['status'] = 0;

        $status = I('get.status',0,'int');
        $point = I('get.integral',0,'int');
        $action = I('get.action',0,'int');

        if(!$action || $action < 1){
            $dataFailure['msg'] = "请指定配置项目";
            $this->ajaxReturn($dataFailure);
        }
        if($point < 0 ){
            $dataFailure['msg'] = "请输入有效值";
            $this->ajaxReturn($dataFailure);
        }
        $data['status'] = $status;
        $data['point'] = $point;

        $res = M('cfg_value_get',null,DB_MAIN_CFG)->where("action = {$action}")->setField($data);
        if($res !== false){
            $dataSuccess['msg'] = "配置成功";
            $this->ajaxReturn($dataSuccess);
        } else{
            $dataFailure['msg'] = "配置出错";
            $this->ajaxReturn($dataFailure);
        }

    }

    /**
     * 系统配置
     */
    public function sysControl(){
        $cfgDefaultFile = 'Application/Common/Conf/defaultConfig.php';
        $localConfigFile = 'data/local_specialConfig.php';
        $fixEnumFile = 'Application/Common/Conf/baseEnum.php';
        //预定义的枚举常量
        $preDefineArr = readConfig($fixEnumFile);

        $defaultArr = readConfig($cfgDefaultFile);
        $localArr   = readConfig($localConfigFile);

        $lastOne = null;
        $lastKey = null;
        $list = array();
        foreach ($defaultArr as $k=>$v){
            if(array_key_exists($lastKey, $localArr) ){
                $localValue=$localArr[$lastKey][0];
            }else{
                $localValue=$lastOne[0];
            }
            $localValue=str_replace('\'','',$localValue);
            $lastOne['localValue'] = $localValue;
            if($lastOne == null){
                $lastOne = dealCurrent($v);
                $lastKey = $k;
                continue;
            }

            if(is_array($lastOne)){
                if(!$lastOne['authority'] || $lastOne['authority'] < 0){
                    if (!$lastOne['format']) {
                        $lastOne['format'] = 'T';
                    }
                    $lastOne['format'] = trim($lastOne['format']);
                    @list($baseType, $ext) = explode('=', $lastOne['format']);
                    switch (strtoupper($baseType)) {
                        case 'PE':
                            $lastOne['type'] = "PE";
                            foreach ($preDefineArr as $pk => $pv) {
                                //$ext=trim($ext);
                                if (stripos($pk, $ext) === false) {
                                    continue;
                                }
                                $pv[0] = str_replace('\'','',$pv[0]);
                                $lastOne['optionT'][] = $pv;
                            }
                            break;
                        case 'EE':
                            $lastOne['type'] = "EE";
                            if ($ext) {
                                $params = explode(',', $ext);
                                foreach ($params as $one) {
                                    @list($tval, $ttxt) = explode(':', $one);
                                    $lastOne['optionT'][$tval] = $ttxt;
                                }
                            }
                            break;
                        case 'I':
                            $lastOne['type'] = "I";
                            break;
                        case 'T':
                            $lastOne['type'] = "T";
                            break;
                        case 'LT':
                            $lastOne['type'] = "LI";
                            break;
                        case 'TT':
                            $lastOne['type'] = "TT";
                            break;
                        default:
                            $lastOne['type'] = "T";
                    }
                    $list[$lastKey] = $lastOne;
                }
                $lastOne = dealCurrent($v);
                $lastKey = $k;
            } else{

                $val = dealCurrent($v);
                //上一个为注释，则判断这个是否不为注释，若不为注释，则保留上一个注释
                if(is_array($val) && $val['authority'] <= 0){
                    $list[$lastKey]['content'] = str_replace('//',"",$lastOne);
                    $list[$lastKey]['flag'] = true;
                }
                $lastOne = $val;
                $lastKey = $k;
            }
        }

        $this->smarty->assign("list",$list);
        $this->smarty->display("Allocation/sysControl.html");
    }
    /*
     * 保存系统配置
     */
    public function saveSysControl(){
        $cfgDefaultFile     = 'Application/Common/Conf/defaultConfig.php';
        $localConfigFile    = 'data/local_specialConfig.php';
        $defaultArr = readConfig($cfgDefaultFile);
        $return = array('status' => 0, 'msg' => 'success');
        writeConfig($localConfigFile, $_POST,$defaultArr);
        $this->ajaxReturn($return);
    }
}